package com.koron.standard.deviceClasssify.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.common.core.util.StringUtils;
import com.koron.standard.deviceClasssify.bean.dto.TypeGroupDTO;
import com.koron.standard.deviceClasssify.bean.po.TypeGroup;
import com.koron.standard.deviceClasssify.bean.po.TypeGroupParams;
import com.koron.standard.deviceClasssify.mapper.TypeGroupMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 设备分类基本参数组(TypeGroup)表服务实现类
 *
 * @author jzp
 * @since 2021-07-13 11:13:26
 */
@Service
public class TypeGroupService extends ServiceImpl<TypeGroupMapper, TypeGroup> {

    @Autowired
    private TypeGroupParamsService typeGroupParamsService;

    @Autowired
    private TypeService typeService;

    @Override
    public List<TypeGroup> list() {
        LambdaQueryWrapper<TypeGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(TypeGroup::getSort);
        return list(wrapper);
    }

    public List<TypeGroupParams> listDefault() {
        return typeGroupParamsService.listDefault();
    }

    public TypeGroupDTO getDtoById(String id) {
        TypeGroupDTO typeGroupDTO = new TypeGroupDTO();

        TypeGroup typeGroup = getById(id);
        if (typeGroup == null) {
            return null;
        }
        BeanUtils.copyProperties(typeGroup, typeGroupDTO);

        List<TypeGroupParams> typeGroupDefaultParamsList = typeGroupParamsService.listDefaultByGroupId(typeGroup.getId());
        List<TypeGroupParams> typeGroupCustomParamsList = typeGroupParamsService.listCustomByGroupId(typeGroup.getId());

        typeGroupDTO.setTypeGroupDefaultParamsList(typeGroupDefaultParamsList);
        typeGroupDTO.setTypeGroupCustomParamsList(typeGroupCustomParamsList);
        return typeGroupDTO;
    }

    /**
     * 保存或更新
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveOrUpdateDto(TypeGroupDTO typeGroupDTO) {
        // 保存基本信息
        if (StringUtils.isEmpty(typeGroupDTO.getId())) {
            // 不允许插入重复数据
            Set<String> collect = list().stream().map(TypeGroup::getName).collect(Collectors.toSet());
            Assert.state(!collect.contains(typeGroupDTO.getName()), "基本参数组名称不能重复");
        }
        saveOrUpdate(typeGroupDTO);

        // 保存所有参数
        typeGroupDTO.getTypeGroupDefaultParamsList().forEach(e -> e.setIsChange(0));
        typeGroupDTO.getTypeGroupCustomParamsList().forEach(e -> e.setIsChange(1));
        typeGroupDTO.getTypeGroupDefaultParamsList().addAll(typeGroupDTO.getTypeGroupCustomParamsList());
        typeGroupParamsService.saveGroup(typeGroupDTO.getTypeGroupDefaultParamsList(), typeGroupDTO.getId());
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteDTO(String id) {
        // 想要判断这个基本参数组有没有被设备分类关联，关联了就不允许删除
        if (typeService.isRefGroup(id)) {
            throw new IllegalArgumentException("该参数组有关联分类标准，不允许删除");
        }

        // 1. 删除该参数组对应参数
        typeGroupParamsService.removeByGroupId(id);

        // 2. 删除该参数组
        removeById(id);
    }
}
